package ru.cjay.loading {
import com.greensock.TweenLite;

import flash.display.Bitmap;
import flash.display.BitmapData;

import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;
import flash.system.SecurityDomain;

import mx.controls.Alert;

public class UnsafeConnection {

    public function UnsafeConnection() {
    }


    public static function UnsafeLoader(url:String, w:int = 0, h:int = 0):Sprite {
        var _sp:Sprite = new Sprite();
        var loader:Loader = new Loader();
        _sp.addChild(loader);
        var loaderContext:LoaderContext = new LoaderContext();
        loaderContext.checkPolicyFile = true;
        loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, function(){
            trace("IOERROR");
        });
        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(event:Event) {
            var l:Loader = event.currentTarget.loader as Loader;
            _sp.addChild(l);
            try{
			    (l.content as Bitmap).smoothing = true;

            }catch(e:Error){
                trace("ERROR WHILE SMOOTHING BITMAP");
            }
            if ((w + h) > 0){
//                var del:Number =  l.content.height/l.content.wi;
//                    (l.content as Bitmap).width = w;
//                    (l.content as Bitmap).height = h;
                _sp.width = w;
                _sp.height = h;
                }

            l.alpha = 0;

            TweenLite.to(l, 0.3, {alpha:1});
        });
        loader.load(new URLRequest(url),loaderContext);
        return _sp;
    }



}
}
